package book.crypto;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

public class SealedObjectTest {

	public static void main(String[] args) throws ClassNotFoundException, IllegalBlockSizeException, BadPaddingException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
		
		KeyGenerator gen =	KeyGenerator.getInstance("des");
		SecretKey key = gen.generateKey();
		
		Cipher c = Cipher.getInstance(key.getAlgorithm());
		c.init(Cipher.ENCRYPT_MODE, key);
		
		String input = "sealedObject";
		SealedObject sealed = new SealedObject(input, c);
		
		Cipher c2 = Cipher.getInstance(key.getAlgorithm());
		c2.init(Cipher.DECRYPT_MODE, key);
		String output = (String)sealed.getObject(c2);
		System.out.println(output);
	}
}
